/**
 * 修改密码
 *
 * @author liuxiaotang
 */
define(['app', 'js/services/MessageBox', 'js/services/MuseService', 'md5'], function (app) {

    var deps = ['$scope', '$state', '$stateParams', 'MessageBox', 'MuseService'];

    function controller ($scope, $state, $stateParams, MessageBox, MuseService) {
        //获取参数
        var adminUserId = $stateParams.adminUserId = $stateParams.adminUserId || '';

        $scope.request = {};

        $scope.save = function () {
            if (isEmpty($scope.request.rawPwd) || isEmpty($scope.request.updatePwd) || isEmpty($scope.request.rePwd)) {
                MessageBox.error('表格尚未填写完整，请填写完后再提交');
                return false;
            } else if ($scope.request.rawPwd == $scope.request.updatePwd) {
                MessageBox.error('新旧密码相同，请重新输入');
                return false;
            } else if ($scope.request.updatePwd != $scope.request.rePwd) {
                MessageBox.error('两次输入的密码不相同，请重新输入');
                return false;
            } else if ($scope.request.updatePwd.length < 6) {
                MessageBox.error('密码长度至少6位');
                return false;
            }
            MuseService.post(baseServices_restfulUrl + 'admin/adminuser/doChangePassword.json?userId=' + adminUserId, {
                rawPwd: hex_md5($scope.request.rawPwd),
                updatePwd: hex_md5($scope.request.updatePwd)
            }).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        MessageBox.notify('修改成功').result.then(function () {
                        }, function () {
                            location.href = webStaticPath + "login.html";
                        });
                    } else {
                        MessageBox.notify(data.message);
                    }
                }).error(function (data) {
                console.log(data);
            });
        };
    }

    controller.$inject = deps;
    app.lazy.controller('ChangePwdController', controller);
});
